//==========================================================================//
//                                                                          //
//  ANEMOMETER Library          Version 0.1                     March 2012  //
//                                                                          //
//  Written for the Arduino Duemilanove w/ ATmega328                        //
//                                                                          //
//  ANEMOMETER Arduino Library                                              //
//                                                                          //
//  Created by JFLagarde (jeanfrancoislagarde@hotmail.com)                  //
//                                                                          //
//  This file is part of OpenWS (http://code.google.com/p/openws/)          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Notes:                                                                  //
//                                                                          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Copyright 2012 Jean-Francois Lagarde (jeanfrancoislagarde@hotmail.com)  //
//  This program is distributed under the terms of                          //
//  the GNU General Public License                                          //
//                                                                          //
//  This program is free software: you can redistribute it and/or modify    //
//  it under the terms of the GNU General Public License as published by    //
//  the Free Software Foundation, either version 3 of the License, or       //
//  (at your option) any later version.                                     //
//                                                                          //
//  This program is distributed in the hope that it will be useful,         //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//  GNU General Public License for more details.                            //
//                                                                          //
//  You should have received a copy of the GNU General Public License       //
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                          //
//==========================================================================//

#ifndef _ANEMOMETER_H_
	#define _ANEMOMETER_H_

	#if defined(ARDUINO) && ARDUINO >= 100
		#include "Arduino.h"
	#else
		#include "WProgram.h"
	#endif

	#define numReadings 40 // 2min/3sec = 40, 10min/3sec = 200
	class ANEMOMETER {
		public:
			ANEMOMETER();
			void init(void);
			void init(byte, byte);
			void read(void);
			unsigned int getWindSpeed(void);
			unsigned int getWindDirection(void);
			unsigned int getWindGust(void);
			unsigned int getMeanWindSpeed(void);
			unsigned int getMeanWindDirection(void);
			
			void calcWind(void);

		private:	
			byte _pinAnemometer;
			byte _pinVane;
			
			unsigned int _lastWindSpeed;
			unsigned int _lastWindDirection;
			unsigned long _lastMillis;
			
			int _index;
			unsigned int _newMinWindSpeed;
			unsigned int _newMaxWindSpeed;
			unsigned int _newMeanWindSpeed;
			unsigned int _newMeanWindDirectionDeg;
			
			//volatile int _numRevs; // Incremented in the interrupt
			
			unsigned int readWindSpeed(void);
			unsigned int readWindDirection(void);
			void static countAnemometer();
			
			
			
			
			
			
			
			//static const int numReadings;
			

			unsigned int _bufferWindSpeed[numReadings];
			int _bufferWindX[numReadings];
			int _bufferWindY[numReadings];

			unsigned long _sumWindSpeed;
			long _sumWindX;
			long _sumWindY;
			
			void bufferWindSpeed(unsigned int, int);
			unsigned int getMinWindSpeed(unsigned int);
			unsigned int getMaxWindSpeed(unsigned int);
			unsigned int getMeanWindSpeedSimple(void);
			unsigned int getMeanWindSpeedComplex(unsigned int, unsigned int, int);
			unsigned int getMeanWindDirectionComplex(void);
			
	};
	int static _numRevs; // Incremented in the interrupt
#endif /*_ANEMOMETER_H_*/